/**
 * 
 */



package com.UMLife.me.action;

import javax.microedition.midlet.MIDlet;
import com.UMLife.me.global.Global;
import com.UMLife.me.model.ForgetPswModel;
import com.UMLife.me.ui.ForgetPswUI;
import de.enough.polish.ui.Alert;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.CommandListener;
import de.enough.polish.ui.Display;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.ImageItem;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemCommandListener;





/**
 * Class Information:
 * 
 * @Description 这个类用来处理忘记密码获得密码界面的事件。
 *              <p>
 *              主要内容有
 *              <p>
 *              1.cmdOk事件响应：从界面处获得内容，然后将内容传送到model层，由model
 *              <p>
 *              层处理这些内容，然后通过返回值判断能否进行新密码的设置。若不能通过新
 *              <p>
 *              密码的设置，则通过Alert通知错误，然后返回到获取密码界面进行重新设置
 *              <p>
 *              。 如果通过了密码验证 ,将返回到登陆界面进行登录。
 *              <p>
 *              2.cmdBack事件响应：返回到登陆界面。
 * @MustDos
 */
public class ForgetPswAction implements CommandListener, ItemCommandListener
{
	public ForgetPswAction(ForgetPswUI forgetPsw, ImageItem button)
	{
		this.init(forgetPsw, button);
		this.addCommand();
		
	}
	
	public void commandAction(Command c, Displayable d)
	{
		
		if (c == this.cmdBack)
		{
			this.display.setCurrent(Global.getLoginUI());
		}
	}
	
	public void commandAction(Command c, Item item)
	{
		if (item.equals(this.button))
		{
			this.okAction();
			
		}
		
	}
	
	private void addCommand()
	{
		this.button.addCommand(this.cmdOk);
		this.button.setItemCommandListener(this);
		this.forgetPsw.addCommand(this.cmdBack);
	}
	
	/**
	 * @param forgetPsw
	 * @param model
	 */
	private void init(ForgetPswUI forgetPsw, ImageItem button)
	{
		this.forgetPsw = forgetPsw;
		this.button = button;
		this.model = Global.getForgetPswModel();
		this.midlet = Global.getGlobalMIDlet();
		this.display = Global.getGlobalDisplay();
		this.cmdOk = new Command("提交修改", Command.OK, 1);
		this.cmdBack = Global.getCmdBack();
	}
	
	/**
	 * 点击cmdOk时激发的事件响应
	 */
	private void okAction()
	{
		boolean legal = this.model.forgetPsw(this.forgetPsw.getPhone(),
				this.forgetPsw.getMail(), this.forgetPsw.getNewPsw());
		if (legal)
		{
			Alert alert = new Alert("提示");
			alert.setString("已经成功的修改了密码");
			alert.setTimeout(ForgetPswAction.alertLastTime);
			
			this.display.setCurrent(alert, Global.getLoginUI());
		}
		if (!legal)
		{
			Alert alert = new Alert("提示");
			alert.setString("您填写的信息有误，不能进行密码的修改");
			alert.setTimeout(ForgetPswAction.alertLastTime);
			this.display.setCurrent(alert, this.forgetPsw);
		}
	}
	
	private static final int	alertLastTime	= 1000;
	
	private ImageItem			button;
	
	private Command				cmdBack;
	
	private Command				cmdOk;
	
	private Display				display;
	
	private ForgetPswUI			forgetPsw;
	
	private MIDlet				midlet;
	
	private ForgetPswModel		model;
	
}
